﻿using Appliaction.Dto.Base_Manage;
using Appliaction.IAppServices.Base_Manage;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class DeptController : ControllerBase
    {
        private readonly IDeptService deptService;
        private readonly IMapper mapper;

        public DeptController(IDeptService deptService,IMapper mapper)
        {
            this.deptService = deptService;
            this.mapper = mapper;
        }

        /// <summary>
        /// 根据部门名称和负责人分页查询信息
        /// </summary>
        /// <param name="dept_name"></param>
        /// <param name="leader"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetDepts(string? dept_name, string? leader, int currentPage, int pageSize)
        {
            return new JsonResult(deptService.GetDepts(dept_name, leader, currentPage, pageSize));
        }

        /// <summary>
        /// 根据部门名称和负责人分页查询总个数
        /// </summary>
        /// <param name="dept_name"></param>
        /// <param name="leader"></param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetDeptsCount(string? dept_name, string? leader)
        {
            return new JsonResult(deptService.GetDeptsCount(dept_name, leader));
        }

        /// <summary>
        /// 查询全部部门
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> DeptOptAsync()
        {
            return new JsonResult(await deptService.DeptOptAsync());
        }

        /// <summary>
        /// 添加部门信息
        /// </summary>
        /// <param name="dept"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> DeptAddAsync(DeptAddDto dept)
        {
            return new JsonResult(await deptService.DeptAddAsync(dept));
        }

        /// <summary>
        /// 修改部门信息
        /// </summary>
        /// <param name="deptUpdateDto"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<JsonResult> DeptUpdateAsync(DeptUpdateDto deptUpdateDto)
        {
            return new JsonResult(await deptService.DeptUpdateAsync(deptUpdateDto));
        }

        /// <summary>
        /// 逻辑删除部门信息
        /// </summary>
        /// <param name="dept_id"></param>
        /// <param name="del"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<JsonResult> DeptDeleteAsync(DeptDelDto deptDelDto)
        {
            return new JsonResult(await deptService.DeptDeleteAsync(deptDelDto));
        }

        /// <summary>
        /// 查询部门信息及其子级部门
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult Get_Dept_Trees(string? name)
        {
            return new JsonResult(deptService.Get_Dept_Trees(name));
        }
    }
}
